﻿using Core.DataAccess.Model;
using Core.DataAccess.Model.Project.Queue;
using Core.Framework.Util.Attributes;
using Core.IBusiness.ISocketModel;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Core.Business.SocketModel
{
    public class SocketGroup : ISocketGroup
    {
        public Tuple<ProjectModuleGroupUser, string, bool> AddGroupUser(ProjectModuleGroupUser model)
        {
            using (var context = new ProjectSocketContext())
            {

                var tuple = model.IsValid(Attributes.IsValidEnum.reg);

                if (!tuple.Item1)
                    return new Tuple<ProjectModuleGroupUser, string, bool>(model, tuple.Item2, false);

                context.ProjectModuleGroupUser.Add(model);
                context.SaveChanges();

                return new Tuple<ProjectModuleGroupUser, string, bool>(model, "添加会员成功", true);
            }

            ;
        }

        public Tuple<ProjectModuleGroup, string, bool> Create(ProjectModuleGroup model)
        {
            using (var context = new ProjectSocketContext())
            {

                var tuple = model.IsValid(Attributes.IsValidEnum.reg);

                if (!tuple.Item1)
                    return new Tuple<ProjectModuleGroup, string, bool>(model, tuple.Item2, false);

                context.ProjectModuleGroup.Add(model);
                context.SaveChanges();

                return new Tuple<ProjectModuleGroup, string, bool>(model, "创建成功", true);
            }

            ;
        }

        public Tuple<ProjectModuleGroup, bool> Delete(int id, string userKey, string projectToken)
        {
            using (var context = new ProjectSocketContext())
            {

                var model = context
                    .ProjectModuleGroup
                    .Where(a => a.Id == id && a.UserId == userKey && a.ProjectToken == projectToken)
                    .FirstOrDefault();
                if (model?.Id > 0)
                {
                    context.ProjectModuleGroup.Remove(model);
                    context.SaveChanges();
                    return new Tuple<ProjectModuleGroup, bool>(model, true);
                }

                return new Tuple<ProjectModuleGroup, bool>(model, false);

            }

            ;
        }

        public Tuple<ProjectModuleGroup, bool> GetById(int id, string projectToken)
        {
            using (var context = new ProjectSocketContext())
            {

                var model = context
                    .ProjectModuleGroup
                    .Where(a => a.Id == id && projectToken == a.ProjectToken)
                    .FirstOrDefault();
                if (model?.Id > 0)
                    return new Tuple<ProjectModuleGroup, bool>(model, true);

                return new Tuple<ProjectModuleGroup, bool>(model, false);

            }

            ;
        }

        public Tuple<List<ProjectModuleGroup>, bool> GetByUserKey(string userKey, string projectToken)
        {
            using (var context = new ProjectSocketContext())
            {

                var list = context
                    .ProjectModuleGroup
                    .Where(a => a.UserId == userKey && a.ProjectToken == projectToken)
                    .ToList();

                return new Tuple<List<ProjectModuleGroup>, bool>(list, true);
            }

            ;
        }

        public Tuple<List<int>, bool> GetGroupsByUserKey(string userKey, string projectToken)
        {
            using (var context = new ProjectSocketContext())
            {

                var list = context
                    .ProjectModuleGroup
                    .Where(a => a.UserId == userKey && a.ProjectToken == projectToken)
                    .Select(a => a.Id)
                    .ToList();

                return new Tuple<List<int>, bool>(list, true);
            }

            ;
        }

        public Tuple<List<ProjectModuleGroupUser>, bool> GetGroupUsersByGroupKey(int id, string projectToken)
        {
            using (var context = new ProjectSocketContext())
            {

                var list = context
                    .ProjectModuleGroupUser
                    .Where(a => a.GroupKey == id && a.ProjectToken == projectToken)
                    .ToList();

                return new Tuple<List<ProjectModuleGroupUser>, bool>(list, true);
            }

            ;
        }

        public Tuple<ProjectModuleGroup, string, bool> Update(ProjectModuleGroup model)
        {
            using (var context = new ProjectSocketContext())
            {

                var tuple = model.IsValid(Attributes.IsValidEnum.update);

                if (!tuple.Item1)
                    return new Tuple<ProjectModuleGroup, string, bool>(model, tuple.Item2, false);

                context.ProjectModuleGroup.Update(model);
                context.SaveChanges();

                return new Tuple<ProjectModuleGroup, string, bool>(model, "修改成功", true);
            }

            ;
        }

        public Tuple<ProjectModuleGroupUser, string, bool> UpdateGroupUser(ProjectModuleGroupUser model)
        {
            using (var context = new ProjectSocketContext())
            {

                var tuple = model.IsValid(Attributes.IsValidEnum.update);

                if (!tuple.Item1)
                    return new Tuple<ProjectModuleGroupUser, string, bool>(model, tuple.Item2, false);

                context.ProjectModuleGroupUser.Update(model);
                context.SaveChanges();

                return new Tuple<ProjectModuleGroupUser, string, bool>(model, "修改成功", true);
            }

            ;
        }
    }
}
